package test1;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

public class TopK {

    public static int[] topK(int[] array,int k ){
        //创建一个大小为k的大根堆
        PriorityQueue<Integer> maxHeap=new PriorityQueue<>(k, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        //遍历数组的元组，前k个元素放到队列中
        for(int i=0;i<array.length;i++){
            if(maxHeap.size() < k){
                maxHeap.offer(array[i]);
            }else{
                if(array[i]<maxHeap.peek()){
                    maxHeap.poll();
                    maxHeap.offer(array[i]);
                }
            }
        }
        int[] tmp=new int[k];
        for(int i=0;i<k;i++){
            tmp[i]=maxHeap.poll();
        }
        return tmp;
    }

    public static void main(String[] args) {
        int[] array={18,21,8,10,34,12};
        int[] x=topK(array, 3);
        System.out.println(Arrays.toString(x));
    }
}